package com.enjoyor.soa.traffic.frame.support.kafka.send.impl;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.SuccessCallback;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.enjoyor.soa.traffic.frame.support.kafka.callback.CallbackFailure;
import com.enjoyor.soa.traffic.frame.support.kafka.callback.CallbackSuccess;
import com.enjoyor.soa.traffic.frame.support.kafka.message.KafkaMessage;
import com.enjoyor.soa.traffic.frame.support.kafka.send.IKafkaSendTool;

@Service
public class KafkaSendTool implements IKafkaSendTool {

    private static final Logger logger = Logger.getLogger(KafkaSendTool.class);

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Override
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void send(String topic, KafkaMessage msg) {
        ListenableFuture<SendResult<String, String>> listenableFuture = this.getkafkaTemplate().send(topic,
                JSON.toJSONString(msg));
        try {
            final SendResult<String, String> sendResult = listenableFuture.get();
            listenableFuture.addCallback(new SuccessCallback() {

                @Override
                public void onSuccess(Object result) {
                    logger.info("kafka Producer发送消息成功！topic=" + sendResult.getRecordMetadata().topic() + ",partition"
                            + sendResult.getRecordMetadata().partition() + ",offset="
                            + sendResult.getRecordMetadata().offset());
                }

            }, new FailureCallback() {

                @Override
                public void onFailure(Throwable ex) {
                    logger.error("kafka Producer发送消息失败！sendResult="
                            + JSONObject.toJSONString(sendResult.getProducerRecord()));
                }

            });
        } catch (Exception e) {
            logger.error(String.format("kafka Producer发送消息失败,异常{%s}", e.getMessage()));
        }
    }

    @Override
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void send(String topic, Integer partition, KafkaMessage msg) {
        ListenableFuture<SendResult<String, String>> listenableFuture = this.getkafkaTemplate().send(topic, partition,
                JSON.toJSONString(msg));
        try {
            final SendResult<String, String> sendResult = listenableFuture.get();
            listenableFuture.addCallback(new SuccessCallback() {

                @Override
                public void onSuccess(Object result) {
                    logger.info("kafka Producer发送消息成功！topic=" + sendResult.getRecordMetadata().topic() + ",partition"
                            + sendResult.getRecordMetadata().partition() + ",offset="
                            + sendResult.getRecordMetadata().offset());
                }

            }, new FailureCallback() {

                @Override
                public void onFailure(Throwable ex) {
                    logger.error("kafka Producer发送消息失败！sendResult="
                            + JSONObject.toJSONString(sendResult.getProducerRecord()));
                }

            });
        } catch (Exception e) {
            logger.error(String.format("kafka Producer发送消息失败,异常{%s}", e.getMessage()));
        }
    }

    @Override
    @SuppressWarnings({ "unchecked" })
    public void send(String topic, KafkaMessage msg, CallbackSuccess callbackSuccess, CallbackFailure callbackFailure) {
        ListenableFuture<SendResult<String, String>> listenableFuture = this.getkafkaTemplate().send(topic,
                JSON.toJSONString(msg));
        try {
            listenableFuture.addCallback(callbackSuccess, callbackFailure);
        } catch (Exception e) {
            logger.error(String.format("kafka Producer发送消息失败,异常{%s}", e.getMessage()));
        }
    }

    protected KafkaTemplate<String, String> getkafkaTemplate() {
        return this.kafkaTemplate;
    }

}
